import Base from './Base.js'

function WechatMp() {
	this.appCode = 'wx_mp_appid'
	this.appCodeKey = 'xshop_wx_mp_appid'
	Base.call(this)
}
WechatMp.prototype = Base.prototype

WechatMp.prototype.constructor = WechatMp

WechatMp.prototype.isLogin = async function() {
	return new Promise((resolve, reject) => {
		wx.checkSession({
			success() {
				resolve(true)
			},
			fail() {
				resolve(false)
			}
		})
	})
}

WechatMp.prototype.getCode = async function() {
	return new Promise((resolve, reject) => {
		wx.login({
			success(res) {
				if (res.code) {
					resolve(res.code)
				} else {
					reject(res)
				}
			}
		})
	})
}

WechatMp.prototype.getScope = async function(code) {
	return new Promise((resolve, reject) => {
		wx.getSetting({
			success(res) {
				if (!res.authSetting[code]) {
					wx.authorize({
						scope: code,
						success() {
							resolve()
						},
						fail(res) {
							resolve()
						}
					})
				} else {
					resolve()
				}
			}
		})
	})
}

WechatMp.prototype.login2 = async function(info = {}) {
	let _this = this
	return new Promise((resolve, reject) => {
		wx.login({
			success(res) {
				if (res.code) {
					info.code = res.code
					console.log(res)
					// uni.setStorage({
					// 	key: 'code',
					// 	data: info.code,
					// })
					// console.log(uni.getStorageSync('code'))
					_this.http.post('/addons/xshop/vendor/login2?vendor=WechatMp', info).then(result => {
						// _this.http.post('/addons/xshop/index/login', info).then(result => {
						resolve(result)
					}).catch(e => {
						reject(e)
					})
				}
			}
		})
	})
}

WechatMp.prototype.login = async function(data = {}) {
	data.openid = uni.getStorageSync('openId')
	// data.code = uni.getStorageSync('code')
	// console.log(uni.getStorageSync('code'))
	return new Promise((resolve, reject) => {
		console.log(data)
		this.http.post('/addons/xshop/vendor/login?vendor=WechatMp', data).then(result => {
			resolve(result)
		}).catch(e => {
			reject(e)
		})
	})
}

export default WechatMp
